<template>
    <div class="songlistbox">
        <!-- 专辑页 -->
        <div v-show="songlistxs">
            <!-- 返回按键， 顶部歌单名 -->
            <mytop v-bind:thisname="allname" v-bind:gdm="alb"></mytop>

            <!-- 歌单详情 -->
            <div class="listtoubox">
                <div class="listbgimg">
                    <div class="zhezhao" ></div>
                    <div class="zzd" :style='{background:`rgb(${bacolor.toString()})`}' ></div>
                    <!-- <img v-lazy="imgs.bgimg" alt=""> -->
                </div>
                <div class="listsmallbox">
                    <div class="listleft">
                        <div class="zjimg">
                            <div class="zj"></div>
                            <div class="otherdw">
                                <div class="zjdw">
                                    <div class="zzjdw"></div>
                                </div>
                            </div>
                        </div>
                        <div class="ltitle">
                            <img :src="imgs.bgimg" id="myimg" @load="loadImage" alt="">
                        </div>
                    </div>
                    <div class="listright">
                        <div class="listtitle">
                            <div class="listtouimg">
                                <div class="zjname">{{imgs.thename}}</div>
                                <div class="zjpeople" @click="xzgs">{{imgs.thepeople}} ›</div>
                            </div>
                        </div>
                        <div class="xiangqing" @click="ckxq">{{imgs.thexq}}</div>
                    </div>
                </div>
            </div>

            <ul class="listcontentbox" ref="ul" :style="{height:xzdg==0?undefined:xzdg+'px'}">
                <li v-for="(item,index) in songarr" :key="item.id">
                    <listone v-bind:myindex="index" v-bind:myid="item.id" v-bind:mypeople="item.people" v-bind:myname="item.name" v-bind:zongid="zjid"></listone>
                </li>
                <li class="loading" v-show="!songlistend">
                    <loading></loading>
                </li>
            </ul>


            <!-- 详情 -->
            <van-popup v-model="showzjxq" closeable :close-on-click-overlay="false" :style="{ height: '100%',width:'100%',background:`rgb(0,0,0,0.23)`,overflowY:'scroll'}"  get-container="#app">
                <div class="zjxqbox">
                    <div class="xqtop">
                        <div class="zjimg">
                            <div class="zj"></div>
                        </div>
                        <div class="ltitle">
                            <img v-lazy="imgs.bgimg" id="myimg" alt="">
                        </div>
                    </div>
                    <div class="xqming">
                        {{imgs.thename}}
                    </div>
                    <div class="xqxian"></div>
                    <div class="myxq">
                        {{imgs.thexq}}
                    </div>
                </div>
            </van-popup> 

            <!-- 歌手选择 -->
            <van-popup v-model="showgeshou" round  get-container="#app" :style="{ width: '70%' ,overflow:'hidden'}">
                <ul class="gslb">
                    <li class="xzgs">
                        请选择要查看的歌手
                    </li>
                    <li class="gs" v-for="item in peoplearr" :key="item.id" @click="gogeshou(item.id)">
                        <p>{{item.name}}</p>
                    </li>    
                </ul>    
            </van-popup>  
        </div>
        <!-- /comment/album?id=32311专辑评论 -->
        <loading v-show="!songlistxs"></loading>
    </div>
</template>
<script>
import mytop from "../small/movetop.vue"
import listone from "../small/listone.vue"
import loading from "../small/loading.vue"
import ColorThief from "../../node_modules/colorthief/dist/color-thief.mjs";

export default {
    name:"album",
    data(){
        return {
            songlistxs:false,
            zjid:0,

            next:1,
            tim:null,
            timer:null,
            timxs:null,
            songlistend:false,

            // 顶部的歌单名
            allname:'',
            alb:0,

            // 歌单详情
            imgs:{
                bgimg:'',
                thename:'',
                thexq:'',
                thepeople:''
            },
            // 当前显示的歌曲列表
            songarr:[],
            // 总歌曲列表
            allnum:[],

            // 歌单高度
            xzdg:1,

            // 专辑创作者列表
            peoplearr:[],
            show: false,
            showgeshou: false,

            // 专辑详情
            showzjxq:false,

            bacolor:'',
        }
    },
    components:{
        loading,
        mytop,
        listone
    },
    created(){
        // console.log(this.$route.query)
        this.zjid = this.$route.query.id
        this.getdata()
        // 创建组件的时候回到最开始的地方
        document.documentElement.scrollTop = 0
        document.body.scrollTop = 0
        // 添加滚动事件
        window.addEventListener('scroll', this.listscroll)
        document.body.addEventListener('scroll', this.listscroll)
    },
    methods:{
        getdata(){
            axios.get(`https://apis.netstart.cn/music/album?id=${this.zjid}`).then(
                res=>{
                    // console.log(res)
                    this.songarr = []
                    if(this.next == 1){
                        this.allname = res.data.album.name
                        this.imgs.bgimg = res.data.album.picUrl
                        this.imgs.thename = res.data.album.name
                        this.imgs.thexq = res.data.album.description
                        let renarr = res.data.album.artists
                        let zjmz = ''
                        for(let c = 0 ;c<renarr.length;c++){
                            if(c == 0){
                                zjmz = renarr[c].name
                            }else{
                                zjmz += "/"+renarr[c].name
                            }
                            let obj = {}
                            obj.name = renarr[c].name
                            obj.id = renarr[c].id
                            this.peoplearr.push(obj)
                        }
                        this.imgs.thepeople = zjmz
                        this.allnum = res.data.songs.length
                        
                        this.gbgd()
                    }
                    let newarr = res.data.songs.slice(0,this.next*10)
                    for(let i = 0;i<newarr.length;i++){
                        let obj ={}
                        obj.name = newarr[i].name
                        obj.id = newarr[i].id
                        let lzmz = ''
                        for(let r = 0 ;r<newarr[i].ar.length;r++){
                            if(r == 0){
                                lzmz = newarr[i].ar[r].name
                            }else{
                                lzmz += "/"+newarr[i].ar[r].name
                            }
                        }
                        obj.people = lzmz
                        this.songarr.push(obj)
                    }
                    if(this.songarr.length == this.allnum){
                        this.songlistend = true
                    }
                },
                err=>{
                    console.log(err)
                }
            )
        },
        listscroll(){
            if(this.$refs.ul !== undefined){
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
                //变量windowHeight是可视区的高度
                let windowHeight = document.documentElement.clientHeight || documentbody.clientHeight;
                if(scrollTop >= 200){
                    if(scrollTop+windowHeight >= 260+this.$refs.ul.scrollHeight-1){
                        clearTimeout(this.timer)
                        this.timer = setTimeout(()=>{
                            this.next += 1
                            // console.log("到了底部");
                            this.getname()
                        },1000)
                    }
                }
            }
        },
        getname(){
            if(this.songlistend == false){
                this.next += 1
                this.getdata()
            }
        },
        gdsong(res){
            // console.log("list",res)
            // this.$emit("send_other",res.id)
            // var ab = []
            // for(let i = 0;i<this.allnum.length;i++){
            //     ab.push(this.allnum[i].id)
            // }
            // console.log(abcdefg)
            // this.$emit("send_list",{
            //     list1:ab,
            //     which:res.which,
            //     what:0
            // })
        },
        gbgd(){
            let windowHeight = document.documentElement.clientHeight || documentbody.clientHeight;
            // console.log(123)
            let uldg = this.$refs.ul.scrollHeight
            if(this.allnum<5){
                this.xzdg = windowHeight-(300-40+30+20)
            }else{
                this.xzdg = 0
            }
        },
        // 歌手
        xzgs(){
            // 如果只有一个歌手的话，就直接跳转页面
            if(this.peoplearr.length == 1){
                this.gogeshou(this.peoplearr[0].id)
            }else{
                // 如果有多个歌手的话，就要进行选择之后再跳歌手页
                this.show = false;
                this.showgeshou = true;
            }
        },
        // 去歌手页
        gogeshou(gsid){
            this.$router.push({path:"/singer",query:{id:gsid}})
        },
        ckxq(){
            this.showzjxq = true;
        },
        loadImage(){//图片加载完成后调用
            let img = document.querySelector('#myimg');
            img.setAttribute('crossOrigin', 'anonymous')
            if (img.complete) {
                let colorthief = new ColorThief();
		        this.bacolor = colorthief.getPalette(img)[0]; 
                this.songlistxs = true
		        // console.log(this.bacolor);
            } else {
                img.addEventListener('load', function () {
                    let colorthief = new ColorThief();
                    this.bacolor = colorthief.getPalette(img)[0]; 
                    this.songlistxs = true
                    // console.log(this.bacolor); 
                });
            }
		    
		},
    }
}
</script>
<style lang="less" scoped>
.top{
    height: 100%;
    width:100%;
}
.songlistbox{
    position: relative;
    height:100%;
    // height:2200px;
}
.listtoubox{
    // z-index: 1;
    width:100%;
    height:300px;
    overflow: hidden;
    position: relative;
    .listbgimg{
        width: 100%;
        position: relative;
        img{
            width: 100%;
        }
    }
    .zhezhao{
        width: 100%;
        height:300px;
        position: absolute;
        top: 0px;
        left:0px;
        background-color: rgba(0, 0, 0, 0.17);
    }
    .zzd{
        width: 100%;
        height:300px;
    }
    .listsmallbox{
        position: absolute;
        width: 100%;
        top: 60%;
        left: 50%;
        transform: translate(-50%,-50%);
        display:flex;
        .listleft{
            width: 50%;
            height:100%;
            border-radius: 10px;
            margin: 20px;
            // border:1px solid white;
            font-size: 0px;
            position: relative;
            // .ltitle{
                img{
                    width: 100%;
                    border-radius: 10px;
                }
            // }
            .zjimg{
                position: absolute;
                // background: red;
                top:-30px;
                left:50%;
                transform: translateX(-50%);
                height:30px;
                width:80px;
                overflow: hidden;
                .zj{
                    width:0px;
                    height:0px;
                    border:40px solid black;
                    border-top-left-radius: 50%;
                    border-top-right-radius: 50%;  
                }
                .otherdw{
                    position: relative;
                    .zjdw{
                        position: absolute;
                        top: -55px;
                        left: 50%;
                        transform: translateX(-10px);
                        height: 30px;
                        width: 80px;
                        overflow: hidden;
                        .zzjdw{
                            width:0px;
                            height:0px;
                            border:10px solid red;
                            border-top-left-radius: 50%;
                            border-top-right-radius: 50%;
                        }
                    }
                }
            }
        }
        .listright{
            // margin-top:20px;
            width: 100%;
            .listtitle{
                color: white;
                // display: flex;
                align-items: center;
                width: 100%;
            }
            .listtouimg{
                .zjname{
                    margin-top:5px;
                    font-size:18px;
                }
                .zjpeople{
                    margin-top:15px;
                    font-size:13px;
                    margin-bottom: 20px;
                    color:rgba(255, 255, 255, 0.658);
                }
            }
            .xiangqing{
                color: white;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
                overflow: hidden;
                font-size:14px;
                width:80%;
            }
        }

    }
}
.listcontentbox{
    z-index: 100;
    width: 100%;
    // height: 300px;
    background-color: #f8f8f8;
    // background-color: red;
    border-top-left-radius: 40px;
    border-top-right-radius: 40px;
    position: relative;
    padding-top:20px;
    margin-top:-40px;
    min-height:70%;
    padding-bottom: 30px;
}
li{
    padding: 10px;
    box-sizing: border-box;
    margin: 10px auto;
    border-radius: 10px;
    background-color: white;
    width: 90%;
    position: relative;
}
.loading{
    background-color: transparent;
    margin-top: 20px;
}
.zjxqbox{
    margin-top: 80px;
    padding-bottom:20px;
    &::-webkit-scrollbar { width: 0 !important }
    .xqtop{
            width: 40%;
            height:100%;
            border-radius: 10px;
            margin: 20px auto;
            // margin: 20px;
            // border:1px solid white;
            font-size: 0px;
            position: relative;
            .ltitle{
                position: relative;
                z-index: 50;
                img{
                    width: 100%;
                    border-radius: 10px;
                }
            }
            .zjimg{
                z-index: 10;
                position: absolute;
                // background: red;
                top:-30px;
                left:50%;
                transform: translateX(-50%);
                height:30px;
                width:120px;
                overflow: hidden;
                .zj{
                    width:0px;
                    height:0px;
                    border:60px solid black;
                    border-top-left-radius: 50%;
                    border-top-right-radius: 50%;  
                }
            }
        }
    .xqxian{
        width:80%;
        height:1px;
        margin:25px auto;
        background: radial-gradient( circle 120px at center center, rgba(138, 138, 138, 0.678),rgba(0, 0, 0, 0));
    }
    .xqming{
        color:white;
        text-align: center;
        font-size:16px;
    }
    .myxq{
        color:rgba(255, 255, 255, 0.8);
        width:80%;
        margin:0 auto;
        line-height:30px;
    }
}
</style>